import { Injectable } from '@angular/core';
import { ServiceBase } from './serviceBase';
import { Router } from '@angular/router';
import { Location } from "@angular/common"

@Injectable()
export class NavigateService extends ServiceBase {
    private history: any[] = [];
    private viewStatusList: ViewStatus[] = [];
    get History(): any[] { return this.history; }

    constructor(
        private location: Location
    ) {
        super();
    }

    private router: Router;
    navigate(pathObj: any[], viewStatus?: ViewStatus) {
        this.clearPageLoadEvent();
        // if (isClear) {
        //     localStorage.removeItem("history");
        //     this.history = [];
        // }
        // let navs = JSON.parse(localStorage.getItem("history"));
        // if (navs && navs.length) {
        //     this.history = navs;
        // }
        // if (pathObj && pathObj.length) {
        //     this.history.push(pathObj);
        //     localStorage.setItem("history", JSON.stringify(this.history));
        //     this.router.navigate(pathObj);
        // }

        if (!this.router) throw "router 没有初始化！";
        if (this.AuthCheck(pathObj[0])) {
            //添加视图参数
            if (viewStatus) {
                if (!this.viewStatusList.length) {
                    this.viewStatusList.push(viewStatus);
                } else {
                    let flag = true;
                    for (let i = 0; i < this.viewStatusList.length; i++) {
                        if (this.viewStatusList[i].key == viewStatus.key) {
                            flag = false;
                            this.viewStatusList[i] = viewStatus;
                            break;
                        }
                    }
                    if (flag) {
                        this.viewStatusList.push(viewStatus);
                    }
                }
            }
            return this.router.navigate(pathObj);
        }
    }

    /**
     * 获取指定的视图参数
     */
    getViewStatus(key) {
        let result = undefined;
        result = this.viewStatusList.find((vs) => {
            return vs.key == key;
        });
        if (!result) {
            return null;
        }
        return result.viewStatus;
    }

    // goBack() {
    //     let navs = JSON.parse(localStorage.getItem("history"));
    //     if (navs && navs.length) {
    //         this.history = navs;
    //     }
    //     let length = this.history.length;
    //     if (this.history) {
    //         if (length > 1) {
    //             this.navigate(this.history[length - 2]);
    //             this.history.splice(length - 2, 2);
    //             localStorage.setItem("history", JSON.stringify(this.history));
    //         } else {
    //             localStorage.removeItem("history");
    //             this.router.navigate(["/"]);
    //         }
    //     }


    // }

    private specialGoBack;

    async goBack() {
        try {
            if (this.specialGoBack) {
                await this.specialGoBack();
            } 
            // else {
            //     await this.defaultGoBack();
            // }

            this.onPageLoad && this.onPageLoad();

        } catch (error) {
            //TODO:
            console.log(error);
        }finally {
            // this.specialGoBack = null;
        }
    }

    registGoBack(specialGoBack: Function) {
        this.specialGoBack = specialGoBack;
    }

    defaultGoBack() {
        //TODO 默认返回事件
        this.location.back();
    }

    registRouter(router: Router) {
        this.router = router;
    }

    AuthCheck(path: string, outRouter?: boolean) {
        if (!(this.CurrentStore || this.StoreOwner)) {
            //没有缓存的店铺和店长信息，重新回到店长登录页面。
            // if (path.indexOf("init") == -1) {
            //     console.log(path);
            //     if (outRouter) {
            //         window.location.href = "/init";
            //     }
            //     else {
            //         this.router.navigate(["init"]);
            //     }

            //     return false;
            // }
        }
        return true;
    }

    onPageLoad: () => void;
    private clearPageLoadEvent() {
        this.onPageLoad = null;
        this.specialGoBack = null;
    }
}

export interface ViewStatus {
    key;
    viewStatus;
}